package com.zmj.demo.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.demo.pojo.entity.DeptEntity;
import com.zmj.demo.pojo.entity.DesignEntity;
import com.zmj.demo.pojo.entity.req.DesignReqDTO;
import com.zmj.demo.pojo.entity.req.DesignDataDTO;
import com.zmj.demo.pojo.entity.req.UserReqDelDTO;
import com.zmj.demo.service.DesignService;
import com.zmj.framework.base.controller.BaseController;
import com.zmj.framework.exception.business.BizException;
import com.zmj.framework.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 * 设计的表单 前端控制器
 * </p>
 *
 * @author wch
 * @since 2024-04-02
 */
@RestController
@Api(tags = { "DesignEntity" })
@RequestMapping("/v1/api/designEntity")
public class DesignController extends BaseController<DesignService, DesignEntity> {



    @ApiOperation(value = "※  design分页查询")
    @Operation(summary = "※ design分页查询", description = "design分页查询")
    @PostMapping(value = "/pageList")
    public Result<IPage<DeptEntity>> pageList(@RequestBody DesignReqDTO designReqDTO) {
        Map<String, Object> param = designReqDTO.getParam() == null ? new HashMap<>() : designReqDTO.getParam();
        param.put("order",designReqDTO.getOrder());
        param.put("orderBy",designReqDTO.getOrderBy());
        IPage<DesignEntity> page = new Page<>(designReqDTO.getPageInfo().getPageIndex(), designReqDTO.getPageInfo().getPageSize());
        page = baseService.selectPageList(page,param);
        return Result.success(page);
    }

    @ApiOperation(value = "※ 新增或编辑保存")
    @Operation(summary = "※ 新增或编辑保存", description = "新增或编辑保存")
    @PostMapping(value = "/saveOrUpdate")
    public Result saveOrUpdate(@Valid @RequestBody DesignEntity designEntity){
        baseService.saveOrUpdate(designEntity);
        return Result.success();
    }

    @ApiOperation(value = "※ 根据id查询desige")
    @Operation(summary = "※ 根据id查询desige", description = "根据id查询desige")
    @GetMapping(value = "/getById/{id}")
    public Result getById(@PathVariable Integer id){
        Assert.notBlank(id.toString(),"id不能为空！");
        DesignEntity designEntity = baseService.getById(id);
        if(Objects.isNull(designEntity)){
            throw new BizException("列表不存在");
        }
        return Result.success(designEntity);
    }

    @ApiOperation(value = "※ 根据id删除")
    @Operation(summary = "※ 根据id删除", description = "根据id删除")
    @PostMapping(value = "/deleteById")
    public Result deleteByIds(@RequestBody UserReqDelDTO userReqDelDTO){
        Assert.notBlank(userReqDelDTO.getIds(),"ids不能为空！");
        List<String> idsStr = Arrays.asList(userReqDelDTO.getIds().split(","));
        List<Integer> ids = idsStr.stream().map(str -> Integer.valueOf(str)).collect(Collectors.toList());
        baseService.removeBatchByIds(ids);
        return Result.success();
    }

    @ApiOperation(value = "※ 编辑数据")
    @Operation(summary = "※ 编辑数据", description = "编辑数据")
    @PostMapping(value = "/editData")
    public Result<?> editData(@Valid @RequestBody DesignDataDTO formDataDTO) {
        DesignEntity designEntity = baseService.getById(formDataDTO.getId());
        if(Objects.isNull(designEntity)){
            throw new BizException("列表不存在");
        }
        baseService.lambdaUpdate()
                .set(DesignEntity::getData,formDataDTO.getData())
                .eq(DesignEntity::getId,formDataDTO.getId())
                .update(new DesignEntity());
        return Result.success();
    }

}
